/*
 * Copyright (C) 2004, Velocityme Partnership
 * AnchorActions.java
 *
 * Created on 25 November 2004, 07:49
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.interfaces.*;
import com.velocityme.valueobjects.*;
import com.velocityme.www.actionforms.nodeactionforms.AnchorValueActionForm;
import com.velocityme.www.dto.IntegerString;
import com.velocityme.www.utility.NodeUtil;
import java.util.*;
import javax.servlet.http.*;
import javax.swing.tree.TreeModel;
import org.apache.struts.action.*;

/**
 *
 * @author  Robert
 */
public class AnchorActions extends TaskActions {
    
    /** Creates a new instance of AnchorActions */
    public AnchorActions() {
    }
    
    public ActionForward show(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        TaskPK taskPK = new TaskPK(nodePK.getNodeId());
        AnchorPK anchorPK = new AnchorPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("interestedContactableValues", cs.getTaskInterestedContactableValues(key, taskPK));
        session.setAttribute("responsibleContactableValues", cs.getTaskResponsibleContactableValues(key, taskPK));
        session.setAttribute("anchorValue", cs.getAnchorValue(key, anchorPK));
        session.setAttribute("anchorHistory", cs.getHistoryValue(key, nodePK));
        return mapping.findForward("ACTION_DISPLAY");
    }
    
    public ActionForward showCreate(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        TaskPK taskPK = new TaskPK(nodePK.getNodeId());
        AnchorPK anchorPK = new AnchorPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("parentNodePK", nodePK);
        Collection priorityIntegerStrings = new ArrayList();
        for (int i = 0; i <= 100; i+=10)
            priorityIntegerStrings.add(new IntegerString(new Integer(i), Integer.toString(i)));
        session.setAttribute("priorityPairs", priorityIntegerStrings);
        Collection ownerIntegerStrings = new ArrayList();
        TreeModel contactableTreeModel = cs.getContactableValueTree(key);
        NodeUtil.recursiveAddContactable(contactableTreeModel, contactableTreeModel.getRoot(), ownerIntegerStrings, 0);
        session.setAttribute("ownerPairs", ownerIntegerStrings);
        AnchorValueActionForm anchorValueActionForm = new AnchorValueActionForm();
        anchorValueActionForm.setOwnerId(key.getPersonPK().getPersonId());
        anchorValueActionForm.setIsInterested(Boolean.TRUE);
        Calendar now = Calendar.getInstance();
        anchorValueActionForm.setYear(now.get(Calendar.YEAR));
        anchorValueActionForm.setMonth(now.get(Calendar.MONTH));
        anchorValueActionForm.setDay(now.get(Calendar.DATE));
        session.setAttribute("anchorActionForm", anchorValueActionForm);
        AnchorValue anchorValue = new AnchorValue();
        anchorValue.setTaskValue(new TaskValue());
        anchorValue.getTaskValue().setNodeValue(new NodeValue());
        session.setAttribute("anchorValue", anchorValue);
        session.setAttribute("years", NodeUtil.getInstance().getYears());
        session.setAttribute("months", NodeUtil.getInstance().getMonths());
        session.setAttribute("days", NodeUtil.getInstance().getDays());
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward showEdit(ActionMapping mapping, 
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        TaskPK taskPK = new TaskPK(nodePK.getNodeId());
        AnchorPK anchorPK = new AnchorPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        AnchorValue anchorValue = cs.getAnchorValue(key, anchorPK);
        session.setAttribute("anchorValue", anchorValue);
        AnchorValueActionForm anchorValueActionForm = new AnchorValueActionForm();
        anchorValueActionForm.setName(anchorValue.getTaskValue().getNodeValue().getName());
        anchorValueActionForm.setDescription(anchorValue.getTaskValue().getNodeValue().getDescription());
        anchorValueActionForm.setPriority(anchorValue.getTaskValue().getPriority());
        anchorValueActionForm.setOwnerId(anchorValue.getTaskValue().getOwnerContactableValue().getContactableId());
        Calendar date = Calendar.getInstance();
        date.setTime(anchorValue.getTaskValue().getStartDate());
        anchorValueActionForm.setYear(date.get(Calendar.YEAR));
        anchorValueActionForm.setMonth(date.get(Calendar.MONTH));
        anchorValueActionForm.setDay(date.get(Calendar.DATE));
        session.setAttribute("anchorActionForm", anchorValueActionForm);
        Collection priorityIntegerStrings = new ArrayList();
        for (int i = 0; i <= 100; i+=10)
            priorityIntegerStrings.add(new IntegerString(new Integer(i), Integer.toString(i)));
        session.setAttribute("priorityPairs", priorityIntegerStrings);
        Collection ownerIntegerStrings = new ArrayList();
        TreeModel contactableTreeModel = cs.getContactableValueTree(key);
        NodeUtil.recursiveAddContactable(contactableTreeModel, contactableTreeModel.getRoot(), ownerIntegerStrings, 0);
        session.setAttribute("ownerPairs", ownerIntegerStrings);
        session.setAttribute("years", NodeUtil.getInstance().getYears());
        session.setAttribute("months", NodeUtil.getInstance().getMonths());
        session.setAttribute("days", NodeUtil.getInstance().getDays());
        session.setAttribute("allContactablePairs", NodeUtil.getAllContactablePairs(key, cs));
        Collection interestedContactableIntegerStrings = new ArrayList();
        Iterator i = cs.getTaskInterestedContactableValues(key, taskPK).iterator();
        while(i.hasNext()) {
            ContactableValue contactableValue = (ContactableValue)i.next();
            interestedContactableIntegerStrings.add(new IntegerString(contactableValue.getContactableId(), ContactableValueToString.toString(contactableValue)));
        }
        session.setAttribute("interestedContactablePairs", interestedContactableIntegerStrings);
        Collection responsibleContactableIntegerStrings = new ArrayList();
        i = cs.getTaskResponsibleContactableValues(key, taskPK).iterator();
        while(i.hasNext()) {
            ContactableValue contactableValue = (ContactableValue)i.next();
            responsibleContactableIntegerStrings.add(new IntegerString(contactableValue.getContactableId(), ContactableValueToString.toString(contactableValue)));
        }
        session.setAttribute("responsibleContactablePairs", responsibleContactableIntegerStrings);
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward edit(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        NodePK nodePK;
        if (request.getParameter("btnSubmit").equals("OK")) {
            AnchorValueActionForm anchorValueActionForm = (AnchorValueActionForm)form;
            AnchorValue anchorValue = (AnchorValue)session.getAttribute("anchorValue");
            anchorValue.getTaskValue().getNodeValue().setName(anchorValueActionForm.getName());
            anchorValue.getTaskValue().getNodeValue().setDescription(anchorValueActionForm.getDescription());
            anchorValue.getTaskValue().setPriority(anchorValueActionForm.getPriority());
            ContactableValue ownerContactableValue = new ContactableValue();
            ownerContactableValue.setPrimaryKey(new ContactablePK(anchorValueActionForm.getOwnerId()));
            anchorValue.getTaskValue().setOwnerContactableValue(ownerContactableValue);
            Calendar date = Calendar.getInstance(); date.clear();
            date.set(anchorValueActionForm.getYear(), anchorValueActionForm.getMonth(), anchorValueActionForm.getDay());
            anchorValue.getTaskValue().setStartDate(date.getTime());
            AnchorPK anchorPK;
            if (anchorValue.anchorIdHasBeenSet()) {
                anchorPK = anchorValue.getPrimaryKey();
                cs.anchorEdit(key, anchorPK, anchorValue, anchorValueActionForm.getResponsibleContactablePKs(), anchorValueActionForm.getInterestedContactablePKs());
            }
            else {
                NodePK parentNodePK = (NodePK)session.getAttribute("parentNodePK");
                anchorPK = cs.anchorCreate(key, parentNodePK, anchorValue);
                if (((AnchorValueActionForm)form).getIsInterested().equals(Boolean.TRUE))
                    cs.taskEnableInterestedContactableStateValue(key, new TaskPK(anchorPK.getAnchorId()));
            }
            nodePK = new NodePK(anchorPK.getAnchorId());
        }
        // Cancel simply displays the original node again
        else {
            nodePK = new NodePK((Integer)session.getAttribute("nodeId"));
        }
        request.setAttribute("nodePK", nodePK);
        return mapping.findForward("ACTION_SUCCESS");
    }
    
}
